#include <iostream>
#include "producer_consumer.hpp"

int main() {
    ProducerConsumer pc;
    pc.run();
    return 0;
}


#if 0
#include <iostream>
#include <functional>
#include <thread>
#include <future>
#include <chrono>
#include <fstream>  
#include <sstream>  
#include <iomanip> 

#include "logger.hpp"
#include "thread_pool.hpp"

using namespace rsv;
std::queue<std::pair<double, double>> dataQueue;
std::mutex mtx;
std::condition_variable cv_producer, cv_consumer;

void producer2() {
    std::future<bool> ret = rsv::ThreadPool::instance().submitTask([]()->bool{
    std::cout << "producer2 start!" << std::endl;
        for (int i = 10; i < 20; ++i) 
        {
            std::unique_lock<std::mutex> lock(mtx);
            cv_producer.wait(lock, []{ return dataQueue.size() < 10; });  // 等待队列不满
            dataQueue.push(std::make_pair(i, i + 1));
            cv_consumer.notify_all();  // 唤醒等待的消费者线程
        }
        std::cout << "producer2 end!" << std::endl;
        return true;
    });

    std::cout << "producer2: " << ret.get() << std::endl;
}

void consumer2() 
{
   std::future<bool> ret = rsv::ThreadPool::instance().submitTask([]()->bool{
        std::cout << "consumer2 start!" << std::endl;
        for (int i = 0; i < 10; ++i) 
        {
             std::pair<double, double> data;
            {
                std::unique_lock<std::mutex> lock(mtx);
                cv_consumer.wait(lock, []{ return !dataQueue.empty(); });  // 等待队列非空
                data = dataQueue.front();
                dataQueue.pop();
                cv_producer.notify_all();  // 唤醒等待的生产者线程
            }
            double result = (data.first * 10 + data.second * 10);
            LOG(Logger::Level::INFO, std::to_string(result));
        }
        std::cout << "consumer2 end!" << std::endl;

        return true;
});

    std::cout << "consumer2: " << ret.get() << std::endl;
}

int main() {
    rsv::ThreadPool::instance().start();

    std::future<bool> retProducer = rsv::ThreadPool::instance().submitTask([]()->bool{
    std::cout << "producer start!" << std::endl;
        for (int i = 0; i < 10; ++i) 
        {
            std::unique_lock<std::mutex> lock(mtx);
            cv_producer.wait(lock, []{ return dataQueue.size() < 10; });  // 等待队列不满
            dataQueue.push(std::make_pair(i, i + 1));
            cv_consumer.notify_all();  // 唤醒等待的消费者线程
        }
        std::cout << "producer end!" << std::endl;
        return true;
    });

    std::future<bool> retConsumer = rsv::ThreadPool::instance().submitTask([]()->bool{
        std::cout << "consumer1 start!" << std::endl;
        for (int i = 0; i < 10; ++i) 
        {
            std::pair<double, double> data;
            {
                std::unique_lock<std::mutex> lock(mtx);
                cv_consumer.wait(lock, []{ return !dataQueue.empty(); });  // 等待队列非空
                data = dataQueue.front();
                dataQueue.pop();
                cv_producer.notify_all();  // 唤醒等待的生产者线程
            }
            double result = (data.first * 10 + data.second * 10);
            LOG(Logger::Level::INFO, std::to_string(result));
        }
        std::cout << "consumer1 end!" << std::endl;

        return true;
    });

    std::thread thProducer2(producer2);
    std::thread thConsumer2(consumer2);

    std::cout << "producer1: " << retProducer.get() << std::endl;
    std::cout << "consumer1: " << retConsumer.get() << std::endl;
    thProducer2.join();
    thConsumer2.join();
    
    return 0;
}
#endif
